package homework.xuluoxi;

/*
 * 作业名称：五子棋游戏
 * 作业功能：实现功能完善的五子棋游戏
 * 学    员：徐罗希
 * 版    本：V1.0
 * 备    注：模块二作业
 */

import java.util.Scanner;

// 为简化代码，代码均在一个类中，且所有方法均为public
public class Gomoku {
    // 定义落子坐标变量以及记录变量
    int x, y, rx, ry;
    // 定义游戏对局是否结束的变量
    boolean end = false;
    // 定义游戏执行顺序的变量
    boolean play = true;
    // 定义接收坐标的接受器
    Scanner sc = new Scanner(System.in);
    // 定义绘制棋盘的二维数组
    char[][] gomokuArr = new char[17][17];
    // 定义储存棋盘上有可能出现的字符的数组
    char[] charArr = {' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', '+', '○', '●'};

    // 初始化表示棋盘的二位数组
    public void initGomokuArr() {
        for (int i = 0; i <= gomokuArr.length - 1; i ++) {
            for (int j = 0; j <= gomokuArr[i].length - 1; j ++) {
                if ((0 == i && 0 == j) || (0 == i && j != 0)) {
                    gomokuArr[i][j] = charArr[j];
                }
                else if (i != 0 && 0 == j) {
                    gomokuArr[i][j] = charArr[i];
                }
                else {
                    gomokuArr[i][j] = charArr[17];
                }
            }
        }
    }
    // 绘制棋盘
    public void show() {
        for (int i = 0; i <= gomokuArr.length - 1; i ++) {
            for (int j = 0; j <= gomokuArr[i].length - 1; j ++) {
                System.out.print(gomokuArr[i][j] + " ");
            }
            System.out.println();
        }
    }
    // 判断落子坐标是否合理
    public void judgeX() {
        for (; ;) {
            x = sc.nextInt();
            rx = x;
            if (x < 0 || x > 15) {
                System.out.print("您输入的横坐标无效，请重新输入：");
            }
            else {
                break;
            }
        }
    }
    public void judgeY() {
        for (; ;) {
            y = sc.nextInt();
            ry = y;
            if (y < 0 || y > 15) {
                System.out.print("您输入的纵坐标无效，请重新输入：");
            }
            else {
                break;
            }
        }
    }
    public boolean judgeXY() {
        if (gomokuArr[x + 1][y + 1] != '+') {
            System.out.println("您输入的位置已经有棋子了，请重新输入！");
            return false;
        }
        else {
            return true;
        }
    }
    // 胜负判断
    // 横向判断
    public void result() {
        int num = 1;
        if (play == true) {
            for(; y <= gomokuArr.length - 3 && gomokuArr[x + 1][y + 2] == charArr[19]; y ++) {
                num ++;
                if(num == 5) {
                    System.out.println("黑方获胜！");
                    end = true;
                    break;
                }
            }
            x = rx;
            y = ry;
            for(; y >= 1 && gomokuArr[x + 1][y] == charArr[19] ; y --) {
                num ++;
                if(num == 5) {
                    System.out.println("黑方获胜！");
                    end = true;
                    break;
                }
            }
            // 纵向判断
            num = 1;
            x = rx;
            y = ry;
            for (; x <= gomokuArr.length - 3 && gomokuArr[x + 2][y + 1] == charArr[19]; x ++) {
                num ++;
                if(num == 5) {
                    System.out.println("黑方获胜！");
                    end = true;
                    break;
                }
            }
            x = rx;
            y = ry;
            for (; x >= 1 && gomokuArr[x][y + 1] == charArr[19]; x --) {
                num ++;
                if(num == 5) {
                    System.out.println("黑方获胜！");
                    end = true;
                    break;
                }
            }
            // 左斜向判断
            num = 1;
            x = rx;
            y = ry;
            for (; x <= gomokuArr.length - 3 && y <= gomokuArr.length - 3 && gomokuArr[x + 2][y + 2] == charArr[19]; ) {
                num ++;
                if(num == 5) {
                    System.out.println("黑方获胜！");
                    end = true;
                    break;
                }
                x ++;
                y ++;
            }
            x = rx;
            y = ry;
            for (; x >= 1 && y >= 1 && gomokuArr[x][y] == charArr[19]; ) {
                num ++;
                if(num == 5) {
                    System.out.println("黑方获胜！");
                    end = true;
                    break;
                }
                x --;
                y --;
            }
            // 右斜向判断
            num = 1;
            x = rx;
            y = ry;
            for (; x <= gomokuArr.length - 3 && y >= 1 && gomokuArr[x + 2][y] == charArr[19]; ) {
                num ++;
                if(num == 5) {
                    System.out.println("黑方获胜！");
                    end = true;
                    break;
                }
                x ++;
                y --;
            }
            x = rx;
            y = ry;
            for (; x >= 1 && y <= gomokuArr.length - 3 && gomokuArr[x][y + 2] == charArr[19]; ) {
                num ++;
                if(num == 5) {
                    System.out.println("黑方获胜！");
                    end = true;
                    break;
                }
                x --;
                y ++;
            }
        }
        else {
            // 横向判断
            for(; y <= gomokuArr.length - 3 && gomokuArr[x + 1][y + 2] == charArr[18]; y ++) {
                num ++;
                if(num == 5) {
                    System.out.println("白方获胜！");
                    end = true;
                    break;
                }
            }
            x = rx;
            y = ry;
            for(; y >= 1 && gomokuArr[x + 1][y] == charArr[18] ; y --) {
                num ++;
                if(num == 5) {
                    System.out.println("白方获胜！");
                    end = true;
                    break;
                }
            }
            // 纵向判断
            num = 1;
            x = rx;
            y = ry;
            for (; x <= gomokuArr.length - 3 && gomokuArr[x + 2][y + 1] == charArr[18]; x ++) {
                num ++;
                if(num == 5) {
                    System.out.println("白方获胜！");
                    end = true;
                    break;
                }
            }
            x = rx;
            y = ry;
            for (; x >= 1 && gomokuArr[x][y + 1] == charArr[18]; x --) {
                num ++;
                if(num == 5) {
                    System.out.println("白方获胜！");
                    end = true;
                    break;
                }
            }
            // 左斜向判断
            num = 1;
            x = rx;
            y = ry;
            for (; x <= gomokuArr.length - 3 && y <= gomokuArr.length - 3 && gomokuArr[x + 2][y + 2] == charArr[18]; ) {
                num ++;
                if(num == 5) {
                    System.out.println("白方获胜！");
                    end = true;
                    break;
                }
                x ++;
                y ++;
            }
            x = rx;
            y = ry;
            for (; x >= 1 && y >= 1 && gomokuArr[x][y] == charArr[18]; ) {
                num ++;
                if(num == 5) {
                    System.out.println("白方获胜！");
                    end = true;
                    break;
                }
                x --;
                y --;
            }
            // 右斜向判断
            num = 1;
            x = rx;
            y = ry;
            for (; x <= gomokuArr.length - 3 && y >= 1 && gomokuArr[x + 2][y] == charArr[18]; ) {
                num ++;
                if(num == 5) {
                    System.out.println("白方获胜！");
                    end = true;
                    break;
                }
                x ++;
                y --;
            }
            x = rx;
            y = ry;
            for (; x >= 1 && y <= gomokuArr.length - 3 && gomokuArr[x][y + 2] == charArr[18]; ) {
                num ++;
                if(num == 5) {
                    System.out.println("白方获胜！");
                    end = true;
                    break;
                }
                x --;
                y ++;
            }
        }
    }
    // 五子棋游戏进行
    public void play() {
        Scanner sc = new Scanner(System.in);
        for (; ;) {
            if (play == true) {
                System.out.println("轮到黑方行动");
                System.out.print("请输入落子的横坐标（0~15）：");
                judgeX();
                System.out.print("请输入落子的纵坐标（0~15）：");
                judgeY();
                if (judgeXY() == false) {
                    continue;
                }
                else{
                    gomokuArr[x + 1][y + 1] = charArr[19];
                    show();
                    result();
                    if (end == true) {
                        break;
                    }
                    play = false;
                }
            }
            else {
                System.out.println("轮到白方行动");
                System.out.print("请输入落子的横坐标（0~15）：");
                judgeX();
                System.out.print("请输入落子的纵坐标（0~15）：");
                judgeY();
                if (judgeXY() == false) {
                    continue;
                }
                else{
                    gomokuArr[x + 1][y + 1] = charArr[18];
                    show();
                    result();
                    if (end == true) {
                        break;
                    }
                    play = true;
                }
            }
        }
    }

    public static void main(String[] args) {
        Gomoku myGo = new Gomoku();
        myGo.initGomokuArr();
        myGo.show();
        myGo.play();
    }
}
